package com.snail.billing.fragment;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonArea;
import com.snail.billing.json.JsonBase;
import com.snail.billing.session.CaptchaSession;
import com.snail.billing.session.PaySnailCardSession;
import com.snail.billing.session.QueryAreaSession;

public class PaySnailCardFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;
	private TextView textAccount;
	private EditText inputCard;
	private EditText inputPassword;
	private EditText inputCaptcha;
	private ImageButton imageCaptcha;
	private View buttonPay;

	private HttpApp httpApp;
	private CaptchaSession captchaSession;
	private QueryAreaSession queryAreaSession;
	private PaySnailCardSession paySession;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_pay_snail_card_activity);
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(DataCache.getInstance().paymentParams.platformName);
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_text_account));
		textAccount.setText("账号: " + DataCache.getInstance().importParams.account);
		inputCard = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_input_card));
		inputPassword = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_input_password));
		inputCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_input_captcha));
		inputCaptcha.setVisibility(View.GONE);
		imageCaptcha = (ImageButton) findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_image_captcha));
		imageCaptcha.setVisibility(View.GONE);
		imageCaptcha.setOnClickListener(this);
		buttonPay = findViewById(ResUtil.getViewId(Resource.id.snailbilling_snail_card_button_pay));
		buttonPay.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		httpApp = new HttpApp(getContext());
		requestCaptcha();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(imageCaptcha)) {
			requestCaptcha();
		} else if (v.equals(buttonPay)) {
			String strCard = inputCard.getText().toString();
			String strPassword = inputPassword.getText().toString();
			String strCaptcha = inputCaptcha.getText().toString();
			if (TextUtils.isEmpty(strCard)) {
				AlertUtil.show(getContext(), "请输入卡号");
				return;
			}
			if (TextUtils.isEmpty(strPassword)) {
				AlertUtil.show(getContext(), "请输入密码");
				return;
			}
			DataCache cache = DataCache.getInstance();
			cache.paymentParams.cardNo = strCard;
			cache.paymentParams.cardPassword = strPassword;
			cache.paymentParams.captchaValue = strCaptcha;
			cache.paymentParams.random = captchaSession.getUuid();
			if (cache.importParams.imprestDestination.equals("1")) {
				requestPay();
			} else if (cache.importParams.imprestDestination.equals("2")) {
				requestArea();
			}
		}
	}

	private void requestCaptcha() {
		captchaSession = new CaptchaSession(1);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(captchaSession);
	}

	private void requestArea() {
		httpApp.setDialogAutoDismiss(false);
		queryAreaSession = new QueryAreaSession();
		httpApp.setOnHttpResultListener(this);
		httpApp.request(queryAreaSession);
	}

	private void requestPay() {
		paySession = new PaySnailCardSession();
		httpApp.setOnHttpResultListener(this);
		httpApp.request(paySession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(captchaSession)) {
				if (captchaSession.isUseCaptcha()) {
					Bitmap bitmap = (Bitmap) captchaSession.getResponseData();
					imageCaptcha.setImageBitmap(bitmap);
					inputCaptcha.setVisibility(View.VISIBLE);
					imageCaptcha.setVisibility(View.VISIBLE);
				}
			} else if (httpSession.equals(queryAreaSession)) {
				JsonArea jsonArea = new JsonArea((String) queryAreaSession.getResponseData());
				if (jsonArea.getCode() == 1) {
					DataCache.getInstance().paymentParams.areaId = jsonArea.getAreaId();
					DataCache.getInstance().paymentParams.areaName = jsonArea.getAreaName();
					requestPay();
				} else {
					httpApp.dialogDismiss();
					Toast.makeText(getContext(), jsonArea.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(paySession)) {
				JsonBase result = new JsonBase((String) paySession.getResponseData());
				if (result.getCode() == 1) {
					getPageManager().forward(PaymentOkFragment.class);
				} else {
					Toast.makeText(getContext(), result.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}
}
